#! /usr/bin/perl

use strict;

use Data::Dumper;

#---------------------------------------------------------------------
#                          parse_makefile_am                          
# 
# Parse (a set of) Makefile.am to compute the Libs: line in the
# pkgconfig-template.pc.in
#---------------------------------------------------------------------

sub parse_makefile_am ()
{
  my %dirs = ( prefix => '@prefix@',
               bin => '@bindir@',
               sbin => '@sbindir@',
               libexec => '@libexecdir@',
               sysconf => '@sysconfdir@',
               sharedstate => '@sharedstatedir@',
               localstate => '@localstatedir@',
               lib => '@libdir@',
               include => '@includedir@',
               oldinclude => '@oldincludedir@',
               dataroot => '@datarootdir@',
               data => '@datadir@',
               info => '@infodir@',
               locale => '@localedir@',
               man => '@mandir@',
               doc => '@docdir@',
               html => '@htmldir@',
               dvi => '@dvidir@',
               pdf => '@pdfdir@',
               ps => '@psdir@' );

  my %libs;
  my %skip;

  local $/;

  while (defined ($_ = <>))
    {
      s/\\\n//g;
      my @lines = split /\n/, $_;

LINE: foreach my $line (@lines)
        {
          next if $line =~ /^#/;

          chomp $line;

          if ($line =~ m%^(\w+)dir\s*=\s*(\S.*?)\s*$%)
            {
              $dirs{$1} ||= $2;
            }
          elsif ($line =~ m%^(\w+)_LTLIBRARIES\s*\+?=\s*(\S.*?)\s*$%)
            {
              my $where = $1;
              my $what = $2;

              next LINE if $where eq "noinst";

              $libs{$where} ||= {};

              scalar map { s/lib(\S+).la/$1/; $libs{$where}->{$_} = 1 } 
                     split /\s+/, $what;
            }
          elsif ($line =~ m%(\w+)_la_LDFLAGS\s*\+?=(.*)%)
	    {
	      my $what = $1;
	      my $flags = $2;

	      if ($flags =~ m%-module%)
		{
		  $what =~ s/^lib//;
		  $skip{$what} = 1;
		}
	    }
        }
    }

  return 
    join " ",
    map { 
          do {
              warn "warning: can't determine lib location for $_\n";
              warn "warning: you'll have to manually add libraries '", (join ", ", keys %{$libs{$_}}), "' to your pkgconfig file (via FW_PKGCONFIG_LIBS_EXTRA)\n"
             } unless $dirs{$_};
          do { "-L" . $dirs{$_} . " " . 
               join " ", map { "-l$_" } grep { ! exists $skip{$_} } keys %{$libs{$_}} } if $dirs{$_};
        }
    keys %libs;
}

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

my $libline = parse_makefile_am ();

print<<EOD
prefix=\@prefix\@
exec_prefix=\@exec_prefix\@
libdir=\@libdir\@
includedir=\@includedir\@

Name: \@FW_PACKAGE_NAME\@
Description: \@FW_PACKAGE_SHORT_DESCRIPTION\@
Version: \@FW_PACKAGE_VERSION\@
Requires: \@FW_PKGCONFIG_REQUIRES\@ \@FW_PKGCONFIG_REQUIRES_EXTRA\@
Libs: $libline \@FW_PKGCONFIG_LIBS_EXTRA\@
Cflags: -I\${includedir}/\@PACKAGE_NAME\@-\@FW_PACKAGE_MAJOR_VERSION\@/ \@FW_PKGCONFIG_CFLAGS_EXTRA\@

\@FW_PKGCONFIG_EXTRA\@
EOD
